package com.ls.blog.utils;

import com.google.gson.Gson;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import java.util.UUID;

public class QianNiuUpload {
    /**
     * 上传图片，用本地路径名直接上传
     * accessKey secretKey, bucket, url这四个参数在配置文件里，用配置类QieNiuProperties
     * @param filePath
     * @return
     */
    public static String uploadImgByLocalFilePath(String filePath,String  accessKey ,String secretKey,String bucket,String url) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传
        //如果是Windows情况下，格式是 D:\\qiniu\\test.png
        String localFilePath = filePath;
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = null;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return url+putRet.key;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 将内存中的字节数组上传到空间中 图片base64 不要,号前面的
     * @param imgStr
     * @return
     */
    public static String uploadImgByBytes(String imgStr){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传
        String accessKey = PictureUtile.accessKey;
        String secretKey =  PictureUtile.secretKey;
        String bucket = PictureUtile.bucketname;
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = null;
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] uploadBytes = decoder.decodeBuffer(imgStr);
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);
            Response response = uploadManager.put(uploadBytes, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            return PictureUtile.url +putRet.key;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 流的形式
     * @param file
     * @return
     */
    public static String uploadFile(MultipartFile file) {
        //构造一个带指定Region对象的配置类  需要设置存储空间
        Configuration cfg = new Configuration(Region.huanan());
        UploadManager uploadManager= new UploadManager(cfg);
        Auth auth = Auth.create(PictureUtile.accessKey, PictureUtile.secretKey);
        String token = auth.uploadToken(PictureUtile.bucketname);
        try {
            String originalFilename = file.getOriginalFilename();
            // 文件后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileKey = UUID.randomUUID().toString() + suffix;
            Response response = uploadManager.put(file.getInputStream(), fileKey, token, null, null);
            return PictureUtile.url + fileKey;}
        catch (Exception e) {
            e.printStackTrace();
            return "null";
        }
    }
}
